package com.dream.testtubebaby_doctor;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;

import com.dream.testtubebaby_doctor.fragment.ConsultationFragment;
import com.dream.testtubebaby_doctor.fragment.OtherFragment;
import com.dream.testtubebaby_doctor.fragment.ProductFragment;
import com.dream.testtubebaby_doctor.fragment.ToldFragment;
import com.dream.testtubebaby_doctor.fragment.WaitingRoomFragment;
import com.dream.testtubebaby_doctor.utils.SharePreferencesManager;
import com.dream.testtubebaby_doctor.utils.StatusBarCompat;

public class MainActivity extends AppCompatActivity {
	private FragmentTabHost mTabHost;
	private LayoutInflater layoutInflater;
	public static final String DEFAULT_INDEX_KEY = "default_index";
	private int default_index = 0;
	private Class<?> fragmentArray[] = { WaitingRoomFragment.class,
			ToldFragment.class, ConsultationFragment.class,
			ProductFragment.class, OtherFragment.class };

	private int mImageViewArray[] = { R.drawable.tab_waiting_room_btn,
			R.drawable.tab_told_btn, R.drawable.tab_consultation_btn,
			R.drawable.tab_else_btn, R.drawable.tab_other_btn };
	private String mTextviewArray[];

	private long firstBackTime;
	private SharePreferencesManager mPreferencesManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPreferencesManager = new SharePreferencesManager(this);
		mPreferencesManager.switchLanguage(mPreferencesManager.readLanguageCode());
		setContentView(R.layout.activity_main);
		StatusBarCompat.compat(this,
				getResources().getColor(R.color.status_bar_color));
		default_index = getIntent().getIntExtra(DEFAULT_INDEX_KEY, 0);
		initView();
		ShareSDK.initSDK(this);
		// ShareSDK.registerPlatform(LaiwangCustomize.class);
		ShareSDK.setConnTimeout(20000);
		ShareSDK.setReadTimeout(20000);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initView() {
		layoutInflater = LayoutInflater.from(this);
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTextviewArray = getResources().getStringArray(R.array.tab_title);

		int count = fragmentArray.length;

		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
					.setIndicator(getTabItemView(i));
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
		}
		mTabHost.setCurrentTab(default_index);
	}

	private View getTabItemView(int index) {
		View view = layoutInflater.inflate(R.layout.tab_item_view, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mTextviewArray[index]);

		return view;
	}

	@Override
	public void onBackPressed() {
		if (firstBackTime + 2000 > System.currentTimeMillis()) {
			super.onBackPressed();
		} else {
			Toast.makeText(getApplicationContext(),
					getResources().getString(R.string.back_finish_msg),
					Toast.LENGTH_SHORT).show();
		}
		firstBackTime = System.currentTimeMillis();
	}
}
